/**
 * File: Course.java
 * Author: Ben Stutsky
 * Team: Bob-omb Squad
 * Date: 4/18/11
 * Email: stutsky1@umbc.edu
 */
package clientApp;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * The Class Course.
 */
public class Course implements Serializable
{
	private static final long serialVersionUID = -4961155488890349570L;

	/** The course code. */
	private String courseCode;
	
	/** The course name. */
	private String courseName;
	
	/** The course description. */
	private String description;
	
	/** The course prereqs. */
	private String[] prereqs;
	
	/** The availability. */
	private int[] availability;
	
	/** The number of credits. */
	private double numCreds;
	
	
	public Course(Course clonee)
	{
		courseCode = clonee.getCourseCode();
		courseName = clonee.getCourseName();
		description = clonee.getDescription();
		setPrereqs(clonee.getPrereqs());
		setAvailability(clonee.getAvailability());
		numCreds = clonee.getNumCreds();
	}
	
	
	/**
	 * Instantiates a new course.
	 *
	 * @param cCode the course code
	 * @param cName the course name
	 * @param desc the description
	 * @param pReqs the prereqs
	 * @param avail the availability
	 * @param creds the number of creditss
	 */
	public Course(String cCode, String cName, String desc, String[] pReqs, int[] avail, double creds)
	{
		courseCode = cCode;
		courseName = cName;
		description = desc;
		setPrereqs(pReqs);
		availability = avail;
		numCreds = creds;
	}

	public Course()
	{
		courseCode = "COURSE CODE";
		courseName = "COURSE NAME";
		description = "Default Description";
		setPrereqs(new String[0]);
		availability = new int[4];
		numCreds = 0;
	}


	/**
	 * Gets the course code.
	 *
	 * @return the course code
	 */
	public String getCourseCode() 
	{
		return courseCode;
	}

	/**
	 * Sets the course code.
	 *
	 * @param cCode the new course code
	 */
	public void setCourseCode(String cCode) 
	{
		this.courseCode = cCode;
	}

	/**
	 * Gets the course name.
	 *
	 * @return the course name
	 */
	public String getCourseName() 
	{
		return courseName;
	}

	/**
	 * Sets the course name.
	 *
	 * @param cName the new course name
	 */
	public void setCourseName(String cName) 
	{
		this.courseName = cName;
	}

	/**
	 * Gets the description.
	 *
	 * @return the description
	 */
	public String getDescription() 
	{
		return description;
	}

	/**
	 * Sets the description.
	 *
	 * @param desc the new description
	 */
	public void setDescription(String desc) 
	{
		this.description = desc;
	}
	
	public boolean addPrereq(String coursePre)
	{
		String[] pre = new String[prereqs.length + 1];
		for(int i = 0; i != prereqs.length; i++)
		{
			pre[i] = prereqs[i];
		}
		pre[prereqs.length] = coursePre;
		prereqs = pre;
		return true;
	}
	
	public boolean removePrereq(String coursePre)
	{
		ArrayList<String> myPre = new ArrayList<String>();
		for(int i = 0; i != prereqs.length; i++)
		{
			if(prereqs[i].compareTo(coursePre) != 0)
			{
				myPre.add(prereqs[i]);
			}
		}
		prereqs = myPre.toArray(new String[myPre.size()]);
		return true;
	}
	
	/**
	 * Gets the prereqs.
	 *
	 * @return the prereqs
	 */
	public String[] getPrereqs() 
	{
		String[] trans = new String[prereqs.length];
		
		for(int i = 0; i != prereqs.length; i++)
		{
			trans[i] = prereqs[i];
		}
			
		return trans;
	}

	/**
	 * Sets the prereqs.
	 *
	 * @param pReqs the new prereqs
	 */
	private void setPrereqs(String[] pReqs) 
	{
		String[] prereqs = new String[pReqs.length];
		for(int i = 0; i != pReqs.length; i++)
		{
			prereqs[i] = pReqs[i];
		}
		
		this.prereqs = pReqs;
	}

	
	public boolean addAvailability(int season)
	{
		int[] avail = new int[availability.length + 1];
		for(int i = 0; i != availability.length; i++)
		{
			avail[i] = availability[i];
		}
		avail[availability.length] = season;
		availability = avail;
		return true;
	}
	
	public boolean removeAvailability(int season)
	{
		ArrayList<Integer> myAvail = new ArrayList<Integer>();
		for(int i = 0; i != availability.length; i++)
		{
			if(availability[i] != season)
			{
				myAvail.add( availability[i]);
			}
		}
		Integer[] myIntegers = myAvail.toArray(new Integer[myAvail.size()]);
		int[] myInts = new int[myIntegers.length];
		int i = 0;
		for(Integer myInt: myIntegers)
		{
			myInts[i] = myInt;
			i++;
		}
		availability = myInts;
		return true;
	}
	
	
	private void setAvailability(int[] avail)
	{
		availability = new int[avail.length];
		for(int i = 0; i != avail.length; i++)
		{
			availability[i] = avail[i];
		}
	}
	
	/**
	 * Gets the availability.
	 *
	 * @return the availability
	 */
	public int[] getAvailability() 
	{
		int[] avail = new int[availability.length];
		for(int i = 0; i != avail.length; i++)
		{
			avail[i] = availability[i];
		}
		return avail;
	}



	/**
	 * Gets the number of credits.
	 *
	 * @return the number of credits
	 */
	public double getNumCreds() 
	{
		return numCreds;
	}

	/**
	 * Sets the number credits.
	 *
	 * @param creds the new number credits
	 */
	public void setNumCreds(int creds) 
	{
		this.numCreds = creds;
	}
	
	//Written by Rotem on April 22
	public String toString() {
		String s = "\n" + courseCode + "\n" + courseName + "\n" + description + "\nAvailability: ";

		for (int a : availability) {
			switch(a) {
			case ProjectConstants.FALL: s += "Fall, "; break;
			case ProjectConstants.WINTER: s += "Winter, "; break;
			case ProjectConstants.SPRING: s += "Spring, "; break;
			case ProjectConstants.SUMMER: s += "Summer, "; break;
			}
		}
		
		s = s.substring(0, s.length() - 2);
		
		s += "\nCredits: " + numCreds + "\nPrerequisites: ";
		
		for (String p : prereqs) {
			s += p + ", ";
		}
		
		return s.substring(0, s.length() - 2) + "\n";
	} //end written by Rotem on April 22
	
	/**
	 * The main method. For unit testing.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args)
	{
		String[] prereqs = {"CMSC104"};
		int[] avail = {ProjectConstants.FALL, ProjectConstants.SPRING};
		Course myCourse = new Course("CMSC201", "Intro to CMSC", "DESC201", prereqs, avail, 4);
		
		System.out.println("Course Code: " + myCourse.getCourseCode());
		System.out.println("Course Name: " + myCourse.getCourseName());
		System.out.println("Course Desc: " + myCourse.getDescription());
		System.out.println("Course Creds: " + myCourse.getNumCreds());
		
		System.out.println("Course prereqs: ");
		String[] coursePre = myCourse.getPrereqs();
		for(int i = 0; i != coursePre.length; i++)
		{
			System.out.println(coursePre[i]);
		}
		
		System.out.println("Course avail: ");
		int[] courseAvail = myCourse.getAvailability();
		for(int i = 0; i != courseAvail.length; i++)
		{
			System.out.println(courseAvail[i]);
		}
		
		System.out.println("\n\nChecking Deep Copy by editing avail and Prereqs\n\n");
		
		coursePre[0] = "Son, I would have been your daddy...";
		courseAvail[0] = ProjectConstants.CMSC;
		
		System.out.println("Course Code: " + myCourse.getCourseCode());
		System.out.println("Course Name: " + myCourse.getCourseName());
		System.out.println("Course Desc: " + myCourse.getDescription());
		System.out.println("Course Creds: " + myCourse.getNumCreds());
		
		System.out.println("Course prereqs: ");
		coursePre = myCourse.getPrereqs();
		for(int i = 0; i != coursePre.length; i++)
		{
			System.out.println(coursePre[i]);
		}
		
		System.out.println("Course avail: ");
		courseAvail = myCourse.getAvailability();
		for(int i = 0; i != courseAvail.length; i++)
		{
			System.out.println(courseAvail[i]);
		}
		
		System.out.println("\n\nTesting clone constructor\n\n");
		
		Course clone = new Course(myCourse);
		
		System.out.println("\nPrinting clone:");
		
		System.out.println("Course Code: " + clone.getCourseCode());
		System.out.println("Course Name: " + clone.getCourseName());
		System.out.println("Course Desc: " + clone.getDescription());
		System.out.println("Course Creds: " + clone.getNumCreds());
		
		System.out.println("Course prereqs: ");
		coursePre = clone.getPrereqs();
		for(int i = 0; i != coursePre.length; i++)
		{
			System.out.println(coursePre[i]);
		}
		
		System.out.println("Course avail: ");
		courseAvail = clone.getAvailability();
		for(int i = 0; i != courseAvail.length; i++)
		{
			System.out.println(courseAvail[i]);
		}
		
		System.out.println("\nPrinting course:");
		
		System.out.println("Course Code: " + myCourse.getCourseCode());
		System.out.println("Course Name: " + myCourse.getCourseName());
		System.out.println("Course Desc: " + myCourse.getDescription());
		System.out.println("Course Creds: " + myCourse.getNumCreds());
		
		System.out.println("Course prereqs: ");
		coursePre = myCourse.getPrereqs();
		for(int i = 0; i != coursePre.length; i++)
		{
			System.out.println(coursePre[i]);
		}
		
		System.out.println("Course avail: ");
		courseAvail = myCourse.getAvailability();
		for(int i = 0; i != courseAvail.length; i++)
		{
			System.out.println(courseAvail[i]);
		}
		
		System.out.println("\n\nChanging Desc in clone\n\n");
		
		clone.setDescription("I have the magic in me.");
		
System.out.println("\nPrinting clone:");
		
		System.out.println("Course Code: " + clone.getCourseCode());
		System.out.println("Course Name: " + clone.getCourseName());
		System.out.println("Course Desc: " + clone.getDescription());
		System.out.println("Course Creds: " + clone.getNumCreds());
		
		System.out.println("Course prereqs: ");
		coursePre = clone.getPrereqs();
		for(int i = 0; i != coursePre.length; i++)
		{
			System.out.println(coursePre[i]);
		}
		
		System.out.println("Course avail: ");
		courseAvail = clone.getAvailability();
		for(int i = 0; i != courseAvail.length; i++)
		{
			System.out.println(courseAvail[i]);
		}
		
		System.out.println("\nPrinting course:");
		
		System.out.println("Course Code: " + myCourse.getCourseCode());
		System.out.println("Course Name: " + myCourse.getCourseName());
		System.out.println("Course Desc: " + myCourse.getDescription());
		System.out.println("Course Creds: " + myCourse.getNumCreds());
		
		System.out.println("Course prereqs: ");
		coursePre = myCourse.getPrereqs();
		for(int i = 0; i != coursePre.length; i++)
		{
			System.out.println(coursePre[i]);
		}
		
		System.out.println("Course avail: ");
		courseAvail = myCourse.getAvailability();
		for(int i = 0; i != courseAvail.length; i++)
		{
			System.out.println(courseAvail[i]);
		}
	}	
}
